package com.example.server.controller.admin;

import com.example.server.common.config.annotation.MustLogin;
import com.example.server.common.config.auth.TokenUser;
import com.example.server.common.constant.Result;
import com.example.server.service.admin.MenuService;
import com.example.server.vo.ButtonVO;
import com.example.server.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-19 15:15
 */
@Api(tags = "Admin菜单相关接口")
@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {
    @Resource
    MenuService menuService;

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "菜单列表", notes = "返回菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "强制更新", name = "force", required = false)
    })
    @GetMapping("/list")
    public Result menuList(Integer force) {
        return menuService.menuList(force);
    }


    @MustLogin(isAdmin = true)
    @ApiOperation(value = "指定角色菜单列表", notes = "指定角色菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
            @ApiImplicitParam(value = "强制更新", name = "force", required = false)
    })
    @GetMapping("/myList")
    public Result menuListByRoleId(Integer roleId, Integer force) {
        return menuService.menuListByRoleId(roleId, force);
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "菜单按钮列表", notes = "返回菜单按钮列表")
    @GetMapping("/buttonList")
    public Result buttonList() {
        return menuService.buttonList();
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "全部按钮列表", notes = "全部按钮列表")
    @GetMapping("/button/list")
    public Result buttonAllList() {
        return menuService.buttonAllList();
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @PostMapping("/add")
    public Result addMenu(@RequestBody MenuVO menuVO, @ApiIgnore TokenUser tokenUser) {
        return menuService.addMenu(menuVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @PostMapping("/update")
    public Result updateMenu(@RequestBody MenuVO menuVO, @ApiIgnore TokenUser tokenUser) {
        return menuService.updateMenu(menuVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "菜单id", name = "menuId", required = true)
    })
    @PostMapping("/delete")
    public Result delMenu(Integer menuId, @ApiIgnore TokenUser tokenUser) {
        return menuService.delMenu(menuId, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "新增按钮", notes = "新增按钮")
    @PostMapping("/button/add")
    public Result addButton(@RequestBody ButtonVO buttonVO, @ApiIgnore TokenUser tokenUser) {
        return menuService.addButton(buttonVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改按钮", notes = "修改按钮")
    @PostMapping("/button/update")
    public Result updateButton(@RequestBody ButtonVO buttonVO, @ApiIgnore TokenUser tokenUser) {
        return menuService.updateButton(buttonVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "删除按钮", notes = "删除按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "按钮id", name = "btnId", required = true)
    })
    @PostMapping("/button/delete")
    public Result delButton(Integer btnId, @ApiIgnore TokenUser tokenUser) {
        return menuService.delButton(btnId, tokenUser.getCustomerId());
    }
}